unit clsItOp_Cep;

interface

uses dm,dbtables,Controls;

type
  TReg_MovOp = record
    cod_op: string;
    horas : TTime;
    valor : double;
  end;

type TVet_MovOp = array of TReg_MovOp;

type TItOp_Cep=class(TObject)
     public
      procedure Gravar(Num_Planilha: string; P_lancamento_imq: int64; Cod_Operador: string);
      procedure Recuperar(Num_Planilha: string; P_lancamento_imq: int64; var tq: TQuery);
      procedure Excluir(Num_Planilha: string; P_lancamento_imq: int64);
      procedure Excluir1(Num_Planilha: string);
      function  Recuperar_Operadores(Num_Planilha: string; P_lancamento_imq: int64): string;
      procedure Listar_Operadores(Num_Planilha: string; P_lancamento_imq: int64; horas: ttime; var vetor: TVet_MovOp);
      procedure Custo_Homem(Num_Planilha: string; P_lancamento_imq: int64; horas: double; var Valor_CH: double);
     end;

implementation

uses clsOperador;

var operador: TOperador;

procedure TItOp_Cep.Excluir;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.Active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Delete from ItOp_Cep where Num_Planilha=:a and lancamento_imq=:b');
 tq.params[0].Asstring := Num_Planilha;
 tq.params[1].AsInteger:= P_lancamento_imq;
 tq.ExecSQL;
 tq.close;
 tq.free;
end;

procedure TItOp_Cep.Excluir1;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.Active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Delete from ItOp_Cep where Num_Planilha=:a');
 tq.params[0].Asstring := Num_Planilha;
 tq.ExecSQL;
 tq.close;
 tq.free;
end;

procedure TItOp_Cep.Gravar;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.Active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from ItOp_Cep where Num_Planilha=:a and lancamento_imq=:b and '+
            'codigo_ope=:c');
 tq.params[0].Asstring := Num_Planilha;
 tq.params[1].AsInteger:= P_lancamento_imq;
 tq.params[2].Asstring := Cod_Operador;
 tq.open;
 if tq.eof then begin
  tq.close;
  tq.SQL.clear;
  tq.Params.clear;
  tq.sql.add('Insert into ItOp_Cep values (:a,:b,:c,:d,:e)');
  tq.params[0].Asstring := Num_Planilha;
  tq.params[1].AsInteger:= P_lancamento_imq;
  tq.params[2].Asstring := Cod_Operador;
  operador              := TOperador.Create;
  operador.Recuperar(Cod_Operador);
  tq.params[3].Asfloat  := operador.Salario;
  tq.params[4].Asfloat  := operador.Outros_Rend;
  operador.free;
  tq.ExecSQL;
 end;
 tq.close;
 tq.free;
end;

procedure TItOp_Cep.Recuperar;
begin
 tq.close;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from ItOp_Cep where Num_Planilha=:a and lancamento_imq=:b');
 tq.params[0].Asstring := Num_Planilha;
 tq.params[1].AsInteger:= P_lancamento_imq;
 tq.open;
end;

function TItOp_Cep.Recuperar_Operadores;
var tq : TQuery;
    res: string;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.Active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from ItOp_Cep where Num_Planilha=:a and lancamento_imq=:b');
 tq.params[0].Asstring := Num_Planilha;
 tq.params[1].AsInteger:= P_lancamento_imq;
 tq.open;
 res:= '';
 while not tq.eof do begin
  if res='' then
   res:= tq.fieldbyname('codigo_ope').asstring
  else
   res:= res+','+tq.fieldbyname('codigo_ope').asstring;
  tq.next;
 end;
 result:= res;
 tq.close;
 tq.free;
end;

procedure TItOp_Cep.Listar_Operadores;
var tq    : TQuery;
    i,xpos: integer;
    achou : boolean;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.Active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from ItOp_Cep where Num_Planilha=:a and lancamento_imq=:b');
 tq.params[0].Asstring := Num_Planilha;
 tq.params[1].AsInteger:= P_lancamento_imq;
 tq.open;
 operador:= TOperador.Create;
 while not tq.eof do begin
  achou:= false;
  xpos := 0;
  for i:= 0 to length(vetor)-1 do begin
   if tq.fieldbyname('codigo_ope').asstring=vetor[i].cod_op then begin
    achou:= true;
    xpos := i;
   end;
  end;
  operador.Recuperar(tq.fieldbyname('codigo_ope').asstring);
  if operador.Num_HorasMes=0 then operador.Num_HorasMes:= 1;
  if not achou then begin
   setlength(vetor,length(vetor)+1);
   vetor[length(vetor)-1].cod_op:= tq.fieldbyname('codigo_ope').asstring;
   vetor[length(vetor)-1].horas := horas;
   vetor[length(vetor)-1].valor := (tq.fieldbyname('salario').asfloat/operador.Num_HorasMes)*(horas*24);
  end else begin
   vetor[xpos].horas            := vetor[xpos].horas+horas;
   vetor[xpos].valor            := vetor[xpos].valor+((tq.fieldbyname('salario').asfloat/operador.Num_HorasMes)*(horas*24));
  end;
  tq.next;
 end;
 operador.free;
 tq.close;
 tq.free;
end;

procedure TItOp_Cep.Custo_Homem;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.Active      := false;
 tq.sql.clear;
 tq.Params.clear;
 tq.sql.add('Select * from ItOp_Cep where Num_Planilha=:a and lancamento_imq=:b');
 tq.params[0].Asstring := Num_Planilha;
 tq.params[1].AsInteger:= P_lancamento_imq;
 tq.open;
 operador:= TOperador.Create;
 while not tq.eof do begin
  operador.Recuperar(tq.fieldbyname('codigo_ope').asstring);
  if operador.Num_HorasMes=0 then operador.Num_HorasMes:= 1;
  Valor_CH:= Valor_CH+((tq.fieldbyname('salario').asfloat/operador.Num_HorasMes)*(horas*24));
  tq.next;
 end;
 operador.free;
 tq.close;
 tq.free;
end;

end.
